package com.cheli.aclservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.cheli.aclservice.client.ManagerClient;
import com.cheli.aclservice.entity.ManaStaff;
import com.cheli.aclservice.service.PermissionService;
import com.cheli.security.entity.SecurityUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 自定义userDetailsService - 认证用户详情
 * </p>
 *
 * @author qy
 * @since 2019-11-08
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private ManagerClient managerClient;

    @Autowired
    private PermissionService permissionService;

    /***
     * 根据账号获取用户信息
     *
     * @param username:
     * @return: org.springframework.security.core.userdetails.UserDetails
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       Object object = managerClient.getStaffById(username).getData().get("staff");

        ManaStaff user = JSON.parseObject(JSON.toJSONString(object), ManaStaff.class);
       com.cheli.security.entity.User curUser = new com.cheli.security.entity.User();
        BeanUtils.copyProperties(user,curUser);

        List<String> authorities = permissionService.selectPermissionValueByUserId(user.getId());
        SecurityUser securityUser = new SecurityUser(curUser);
        securityUser.setPermissionValueList(authorities);
        return securityUser;
    }

}
